﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotionControl;
using WHW_NetUtility;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace MainFrame
{
    class GlobalMotion
    {
        //全局运动变量
        public static CardMotion mainCard = (CardMotion)GlobalVariable.ListCard[0];
        public static string cardName = "DMC3800";
        public static bool[] cardInput = new bool[16];
        public static bool[] cardOutput = new bool[16];


        /// <summary>
        /// 获取所有电机的当前位置
        /// </summary>
        /// <returns>电机数位数组</returns>
        public static string[] getCurrent()
        {
            try
            {
                string[] AxisValue = new string[GlobalMotion.mainCard.AxisList.Count];
                for (int i = 0; i < AxisValue.Length; i++)
                {
                    AxisValue[i] = GlobalMotion.mainCard.AxisList[i].m_AxisSts.CurrPosi.ToString();
                }
                return AxisValue;
            }
            catch (System.Exception ex)
            {
                displalyInfo.displayInfoMain("GlobalMotion.getCurrent出错：" + Environment.NewLine + ex.Message);
                Log4Helper.WriteLog("GlobalMotion.getCurrent", ex);
                return null;
            }
        }
        /// <summary>
        /// 运动到位置 单轴
        /// </summary>
        /// <param name="position1">轴距离</param>
        /// <param name="axisNo">轴号</param>
        public static void toPosition(double position, int axisOne, double VelRate)
        {
            Stopwatch getPositionSW = new Stopwatch();
            try
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//先停止轴
                //if (VelRate <= 0 || VelRate > 1)
                //{
                //    VelRate = 0.5;
                //}
                if (!GlobalVar.isEstop && !GlobalVar.isGiveUp && !GlobalVar.isHome && !GlobalVar.isPause && !GlobalVar.isExit)
                {
                    GlobalMotion.mainCard.AxisList[axisOne].PointMove(position, VelRate, true);
                    getPositionSW.Restart();
                    while (true)
                    {
                        getPositionSW.Start();
                        Thread.Sleep(20);
                        if (mainCard.AxisList[axisOne].m_AxisSts.Arrive )
                        {
                            //displalyInfo.displayInfoMain("已运行到指定位置");
                            break;
                        }
                        if (getPositionSW.ElapsedMilliseconds > 10000)
                        {
                            displalyInfo.displayInfoMain("运行到指定位置超时，请检查");
                            return;
                        }
                        if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isExit)
                        {
                            MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//出错时，停止轴

                Log4Helper.WriteLog("GlobalMotion.toPosition", ex);
                displalyInfo.displayInfoMain("GlobalMotion.toPosition 出错" + Environment.NewLine + ex.Message);
            }

        }
        public static void toChangePosition(double position, int axisOne, double VelRate)
        {
            Stopwatch getPositionSW = new Stopwatch();
            try
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//先停止轴
                //if (VelRate <= 0 || VelRate > 1)
                //{
                //    VelRate = 0.5;
                //}
                if (!GlobalVar.isEstop && !GlobalVar.isGiveUp && !GlobalVar.isHome && !GlobalVar.isPause && !GlobalVar.isExit)
                {
                    GlobalMotion.mainCard.AxisList[axisOne].PointMoveChangePos(position, VelRate, true);
                    getPositionSW.Restart();
                    while (true)
                    {
                        getPositionSW.Start();
                        Thread.Sleep(20);
                        if (mainCard.AxisList[axisOne].m_AxisSts.Arrive)
                        {
                            //displalyInfo.displayInfoMain("已运行到指定位置");
                            break;
                        }
                        if (getPositionSW.ElapsedMilliseconds > 10000)
                        {
                            displalyInfo.displayInfoMain("运行到指定位置超时，请检查");
                            return;
                        }
                        if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isExit)  //20190718改，取消暂停时立即停止  下同
                        {
                            MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        }
     
                    }
                }
            }
            catch (System.Exception ex)
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//出错时，停止轴

                Log4Helper.WriteLog("GlobalMotion.toPosition", ex);
                displalyInfo.displayInfoMain("GlobalMotion.toPosition 出错" + Environment.NewLine + ex.Message);
            }

        }
        public static void toPosition(double position, int axisOne)
        {
            Stopwatch getPositionSW = new Stopwatch();
            try
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//先停止轴

                if (!GlobalVar.isEstop && !GlobalVar.isGiveUp && !GlobalVar.isHome && !GlobalVar.isPause && !GlobalVar.isExit)
                {
                    GlobalMotion.mainCard.AxisList[axisOne].PointMove(position, false);
                    getPositionSW.Restart();
                    while (true)
                    {
                        getPositionSW.Start();
                        Thread.Sleep(20);
                        if (mainCard.AxisList[axisOne].m_AxisSts.Arrive)
                        {
                            //displalyInfo.displayInfoMain("已运行到指定位置");
                            break;
                        }
                        if (getPositionSW.ElapsedMilliseconds > 10000)
                        {
                            displalyInfo.displayInfoMain("运行到指定位置超时，请检查");
                            return;
                        }
                        if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isExit)
                        {
                            MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        }
   
                    }
                }
            }
            catch (System.Exception ex)
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//出错时，停止轴

                Log4Helper.WriteLog("GlobalMotion.toPosition", ex);
                displalyInfo.displayInfoMain("GlobalMotion.toPosition 出错" + Environment.NewLine + ex.Message);
            }

        }
        public static void toChangePosition(double position, int axisOne)
        {
            Stopwatch getPositionSW = new Stopwatch();
            try
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//先停止轴

                if (!GlobalVar.isEstop && !GlobalVar.isGiveUp && !GlobalVar.isHome && !GlobalVar.isPause && !GlobalVar.isExit)
                {
                    GlobalMotion.mainCard.AxisList[axisOne].PointMoveChangePos(position, false);
                    getPositionSW.Restart();
                    while (true)
                    {
                        getPositionSW.Start();
                        Thread.Sleep(20);
                        if (mainCard.AxisList[axisOne].m_AxisSts.Arrive)
                        {
                            //displalyInfo.displayInfoMain("已运行到指定位置");
                            break;
                        }
                        if (getPositionSW.ElapsedMilliseconds > 10000)
                        {
                            displalyInfo.displayInfoMain("运行到指定位置超时，请检查");
                            return;
                        }
                        if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isExit)
                        {
                            MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        }
                        //if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isPause || GlobalVar.isExit)
                        //{
                        //    MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        //}
                    }
                }
            }
            catch (System.Exception ex)
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//出错时，停止轴

                Log4Helper.WriteLog("GlobalMotion.toPosition", ex);
                displalyInfo.displayInfoMain("GlobalMotion.toPosition 出错" + Environment.NewLine + ex.Message);
            }

        }
        /// <summary>
        /// 运动到位置 三轴
        /// </summary>
        /// <param name="position1">1轴距离,只代表序号，非实际轴号</param>
        /// <param name="position2">2轴距离，只代表序号，非实际轴号</param>
        /// <param name="position3">3轴距离，只代表序号，非实际轴号</param>
        /// <param name="axiOne">轴号,只代表序号，非实际轴号</param>
        /// <param name="axisTwo">轴号,只代表序号，非实际轴号</param>
        /// <param name="axisThree">轴号,只代表序号，非实际轴号</param>
        public static void toPosition(double position1,double positon2,double position3,int axisOne,int axisTwo,int axisThree,double VelRate)
        {
            Stopwatch getPositionSW=new Stopwatch();
            try
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//先停止轴
                GlobalMotion.mainCard.AxisList[axisTwo].AxisStop();
                GlobalMotion.mainCard.AxisList[axisThree].AxisStop(); 
                if (VelRate<=0|| VelRate>1)
                {
                    VelRate = 0.5;
                }
                if (!GlobalVar.isEstop && !GlobalVar.isGiveUp && !GlobalVar.isHome && !GlobalVar.isPause && !GlobalVar.isExit)
                {
                    GlobalMotion.mainCard.AxisList[axisOne].PointMove(position1, VelRate, true);
                    GlobalMotion.mainCard.AxisList[axisTwo].PointMove(positon2, VelRate, true);
                    GlobalMotion.mainCard.AxisList[axisThree].PointMove(position3, VelRate, true);
                    getPositionSW.Restart();
                    while (true)
                    {
                        getPositionSW.Start();
                        Thread.Sleep(20);
                        if (mainCard.AxisList[axisOne].m_AxisSts.Arrive && mainCard.AxisList[axisTwo].m_AxisSts.Arrive && mainCard.AxisList[axisThree].m_AxisSts.Arrive)
                        {
                            //displalyInfo.displayInfoMain("已运行到指定位置");
                            break;
                        }
                        if (getPositionSW.ElapsedMilliseconds > 5000)
                        {
                            displalyInfo.displayInfoMain("运行到指定位置超时，请检查");
                            return;
                        }
                        if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isExit)
                        {
                            MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        }
                        //if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isPause || GlobalVar.isExit)
                        //{
                        //    MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        //}
                    }
                }
            }
            catch (System.Exception ex)
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//出错时，停止轴
                GlobalMotion.mainCard.AxisList[axisTwo].AxisStop();
                GlobalMotion.mainCard.AxisList[axisThree].AxisStop();
                Log4Helper.WriteLog("GlobalMotion.toPosition", ex);
                displalyInfo.displayInfoMain("GlobalMotion.toPosition 出错"+Environment.NewLine+ex.Message);
            }

        }

        /// <summary>
        /// 运动到位置 两轴
        /// </summary>
        /// <param name="X">X轴距离</param>
        /// <param name="Y">Y轴距离</param>
        /// <param name="axisNo">轴号（0或2）</param>
        /// <param name="axisNoC">轴号（1或3）</param>
        public static void toPosition(double X, double Y, int axisNo, int axisNoC)
        {
            Stopwatch getPositionSW = new Stopwatch();
            try
            {
                GlobalMotion.mainCard.AxisList[axisNo].AxisStop();//先停止轴
                GlobalMotion.mainCard.AxisList[axisNoC].AxisStop();
                if (GlobalVar.typedata_g.getMagneticVelRate <= 0 || GlobalVar.typedata_g.getMagneticVelRate > 1)
                {
                    GlobalVar.typedata_g.getMagneticVelRate = 0.5;
                }
                if (!GlobalVar.isEstop && !GlobalVar.isGiveUp && !GlobalVar.isHome && !GlobalVar.isPause && !GlobalVar.isExit)
                {
                    GlobalMotion.mainCard.AxisList[axisNo].PointMove(X, GlobalVar.typedata_g.getMagneticVelRate, true);
                    GlobalMotion.mainCard.AxisList[axisNoC].PointMove(Y, GlobalVar.typedata_g.getMagneticVelRate, true);
                    getPositionSW.Restart();
                    while (true)
                    {
                        getPositionSW.Start();
                        Thread.Sleep(20);
                        if (mainCard.AxisList[axisNo].m_AxisSts.Arrive && mainCard.AxisList[axisNoC].m_AxisSts.Arrive)
                        {
                            //displalyInfo.displayInfoMain("已运行到指定位置");
                            break;
                        }
                        if (getPositionSW.ElapsedMilliseconds > 5000)
                        {
                            displalyInfo.displayInfoMain("运行到指定位置超时，请检查");
                            return;
                        }
                        //if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isPause || GlobalVar.isExit)
                        //{
                        //    MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        //}
                        if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isExit)
                        {
                            MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                GlobalMotion.mainCard.AxisList[axisNo].AxisStop();//出错时，停止轴
                GlobalMotion.mainCard.AxisList[axisNoC].AxisStop();
                Log4Helper.WriteLog("GlobalMotion.toPosition", ex);
                displalyInfo.displayInfoMain("GlobalMotion.toPosition 出错" + Environment.NewLine + ex.Message);
            }

        }

        /// <summary>
        /// 运动到位置 两轴
        /// </summary>
        /// <param name="position1">1轴距离,只代表序号，非实际轴号</param>
        /// <param name="position2">2轴距离，只代表序号，非实际轴号</param>
        /// <param name="axiOne">轴号,只代表序号，非实际轴号</param>
        /// <param name="axisTwo">轴号,只代表序号，非实际轴号</param>
        /// <param name="VelRate">相对速率</param>
        public static void toPosition(double position1, double position2, int axisOne, int axisTwo,double VelRate)
        {
            Stopwatch getPositionSW = new Stopwatch();
            try
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//先停止轴
                GlobalMotion.mainCard.AxisList[axisTwo].AxisStop();
                if (VelRate <= 0 || VelRate > 1)
                {
                    VelRate = 0.5;
                }
                if (!GlobalVar.isEstop && !GlobalVar.isGiveUp && !GlobalVar.isHome && !GlobalVar.isPause && !GlobalVar.isExit)
                {
                    GlobalMotion.mainCard.AxisList[axisOne].PointMove(position1, VelRate, true);
                    GlobalMotion.mainCard.AxisList[axisTwo].PointMove(position2, VelRate, true);
                    getPositionSW.Restart();
                    while (true)
                    {
                        getPositionSW.Start();
                        Thread.Sleep(20);
                        if (mainCard.AxisList[axisOne].m_AxisSts.Arrive && mainCard.AxisList[axisTwo].m_AxisSts.Arrive)
                        {
                            //displalyInfo.displayInfoMain("已运行到指定位置");
                            break;
                        }
                        if (getPositionSW.ElapsedMilliseconds > 5000)
                        {
                            displalyInfo.displayInfoMain("运行到指定位置超时，请检查");
                            return;
                        }
                        //if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isPause || GlobalVar.isExit)
                        //{
                        //    MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        //}
                        if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isExit)
                        {
                            MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                GlobalMotion.mainCard.AxisList[axisOne].AxisStop();//出错时，停止轴
                GlobalMotion.mainCard.AxisList[axisTwo].AxisStop();
                Log4Helper.WriteLog("GlobalMotion.toPosition", ex);
                displalyInfo.displayInfoMain("GlobalMotion.toPosition 出错" + Environment.NewLine + ex.Message);
            }

        }
        public static void toPositionChange(double X, double Y, int axisNo, int axisNoC)
        {
            Stopwatch getPositionSW = new Stopwatch();
            try
            {
                GlobalMotion.mainCard.AxisList[axisNo].AxisStop();//先停止轴
                GlobalMotion.mainCard.AxisList[axisNoC].AxisStop();
                if (GlobalVar.typedata_g.getMagneticVelRate <= 0 || GlobalVar.typedata_g.getMagneticVelRate > 1)
                {
                    GlobalVar.typedata_g.getMagneticVelRate = 0.5;
                }
                if (!GlobalVar.isEstop && !GlobalVar.isGiveUp && !GlobalVar.isHome && !GlobalVar.isPause && !GlobalVar.isExit)
                {
                    GlobalMotion.mainCard.AxisList[axisNo].PointMoveChangePos(X, GlobalVar.typedata_g.getMagneticVelRate, true);
                    GlobalMotion.mainCard.AxisList[axisNoC].PointMoveChangePos(Y, GlobalVar.typedata_g.getMagneticVelRate, true);
                    getPositionSW.Restart();
                    while (true)
                    {
                        getPositionSW.Start();
                        Thread.Sleep(20);
                        if (mainCard.AxisList[axisNo].m_AxisSts.Arrive && mainCard.AxisList[axisNoC].m_AxisSts.Arrive)
                        {
                            //displalyInfo.displayInfoMain("已运行到指定位置");
                            break;
                        }
                        if (getPositionSW.ElapsedMilliseconds > 5000)
                        {
                            displalyInfo.displayInfoMain("运行到指定位置超时，请检查");
                            return;
                        }
                        //if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isPause || GlobalVar.isExit)
                        //{
                        //    MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        //}
                        if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isExit)
                        {
                            MotionControl.GlobalVariable.HOperation.AllAxisStop(GlobalMotion.mainCard.m_CardID);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                GlobalMotion.mainCard.AxisList[axisNo].AxisStop();//出错时，停止轴
                GlobalMotion.mainCard.AxisList[axisNoC].AxisStop();
                Log4Helper.WriteLog("GlobalMotion.toPosition", ex);
                displalyInfo.displayInfoMain("GlobalMotion.toPosition 出错" + Environment.NewLine + ex.Message);
            }

        }

        public static void getAxisAndSet(TextBox tbx, TextBox tbx1, int axisNo, int axisNoTwo)
        {
            try
            {
                string[] axisCurrentValue;
                axisCurrentValue = GlobalMotion.getCurrent();
                if (axisCurrentValue != null)
                {
                    tbx.Text = axisCurrentValue[axisNo];
                    tbx1.Text = axisCurrentValue[axisNoTwo];
                }
            }
            catch (System.Exception ex)
            {
                displalyInfo.displayInfoMain("FormType.getAxisAndSet" + ex.Message);
            }
        }

        public static void CheckIO()
        {
            //mainCard.m_BlnInputSts
        }

        public static bool GoHome()
        {
            Stopwatch axisSW = new Stopwatch();

            if (!GlobalVar.isEstop && !GlobalVar.isGiveUp && !GlobalVar.isPause && !GlobalVar.isExit)
            {
                
                GlobalMotion.mainCard.AxisList[3].GoHome();
                axisSW.Restart();
                while (true)
                {
                    if (GlobalMotion.mainCard.AxisList[3].m_AxisSts.HomeFlag)
                    {
                        displalyInfo.displayInfoMain("Z轴回原完成，开始其它轴回原");
                        break;
                    }
                    if (axisSW.ElapsedMilliseconds>5000)
                    {
                        displalyInfo.displayInfoMain("Z轴回原超时！");
                        GlobalVar.errorTips[(int)GlobalVar.errorType.homeError].haveInfo = true;
                        GlobalVar.errorTips[(int)GlobalVar.errorType.homeError].Info = "Z轴回原失败";
                        break;
                    }
                    if (GlobalVar.isEstop ||GlobalVar.isGiveUp ||GlobalVar.isExit)
                    {
                        MotionControl.GlobalVariable.HOperation.AllAxisStop(mainCard.m_CardID);
                        return false;
                    }
                    //if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isPause || GlobalVar.isExit)
                    //{
                    //    MotionControl.GlobalVariable.HOperation.AllAxisStop(mainCard.m_CardID);
                    //    return false;
                    //}
                }
                GlobalMotion.mainCard.AxisList[0].GoHome();
                GlobalMotion.mainCard.AxisList[1].GoHome();
                GlobalMotion.mainCard.AxisList[2].GoHome();
                GlobalMotion.mainCard.AxisList[4].GoHome();
                axisSW.Restart();
                while (true)
                {
                    if (GlobalMotion.mainCard.AxisList[0].m_AxisSts.HomeFlag&& GlobalMotion.mainCard.AxisList[1].m_AxisSts.HomeFlag&& 
                        GlobalMotion.mainCard.AxisList[2].m_AxisSts.HomeFlag&& GlobalMotion.mainCard.AxisList[4].m_AxisSts.HomeFlag)
                    {
                        displalyInfo.displayInfoMain("全部轴回原完成");
                        break;
                    }
                    if (axisSW.ElapsedMilliseconds > 10000)
                    {
                        displalyInfo.displayInfoMain("回原超时！");
                        GlobalVar.errorTips[(int)GlobalVar.errorType.homeError].haveInfo = true;
                        GlobalVar.errorTips[(int)GlobalVar.errorType.homeError].Info = "回原超时";
                        break;
                    }
                    if (GlobalVar.isEstop || GlobalVar.isGiveUp || GlobalVar.isPause || GlobalVar.isExit)
                    {
                        MotionControl.GlobalVariable.HOperation.AllAxisStop(mainCard.m_CardID);
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }


      
        public static Task<bool> ToPosition_Get()
        {
            Task<bool> result= Task.Factory.StartNew(() =>
            {
                try
                {
                    //MotionControl.GlobalVariable.HOperation.SetOut(GlobalMotion.mainCard.m_CardID, (ushort)0, 1);//关真空，暂停重启会掉下来
                    //MotionControl.GlobalVariable.HOperation.SetOut(GlobalMotion.mainCard.m_CardID, (ushort)1, 1);

                    if (GlobalMotion.mainCard.AxisList[3].m_AxisSts.CurrPosi > GlobalVar.typedata_g.ZsafePosition)
                    {
                        GlobalMotion.toPosition(GlobalVar.typedata_g.ZsafePosition, 3, GlobalVar.typedata_g.getMagneticVelRate);
                    }
                    GlobalMotion.toPosition(GlobalVar.typedata_g.getMagneticY, GlobalVar.typedata_g.getMagneticW,
                         1, 4, GlobalVar.typedata_g.getMagneticVelRate);
                    return true;
                }
                catch (Exception ex)
                {
                    displalyInfo.displayInfoMain("到取磁片位异常："+ex.Message);
                    return false;
                }
               
            });
            return result;
        }

    }
}
